package com.stlm2.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by apple on 2017/5/28.
 */
public class RegularMatchUtils {
    private static final String PHONE_NUMBER_REGULAR = "^(((\\+|00)?86|\\((\\+|00)?86\\))(\\s|-)?)?1[3-9][0-9]\\s?[0-9]{4}\\s?[0-9]{4}$";   //手机号码正则表达式+86 186 **** ****
    private static final String PHONE_NUMBER_NARROW_REGULAR = "^1[3-9][0-9]{9}$";   //手机号码正则表达式186********
    private static final String MOBILE_NUMBER_NARROW_REGULAR = "^(\\d{3,4}(\\s|-)?)?\\d{6,8}|(((\\+|00)?86|\\((\\+|00)?86\\))(\\s|-)?)?1[3-9][0-9]\\s?[0-9]{4}\\s?[0-9]{4}$";   //手机号码正则表达式186********
    private static final String ZIP_CODE_REGULAR = "^\\d{6}$";   //邮政编码450000
    private static final String E_MAIL_REGULAR = "^\\w{3,20}@\\w+(\\.\\w+){1,2}$";   //邮箱正则
    private static final String NUMBER_PLATES_REGULAR = "^[\\u4e00-\\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$";       //车牌号
    private static final String UPLOAD_URL = "^[\\/a-zA-Z0-9\\.,]+$";       //上传的图片URL
    private static final String BANK_NO = "^[1-9]\\d{15,25}$";      //银行卡号
    private static final String ALPHANUMERIC_CHARACTER  = "^(?=([a-zA-Z])*\\d([a-zA-Z])*)(?=\\d*[a-zA-Z]\\d*)[a-zA-Z0-9]*$";      //必须包含数字和字母的字符串
    private static final String ID_CARD_NUMBER  = "^[1-9]\\d{16}(\\d|(x|X))$";      //身份证号

    private static final String MULTIPLES_OF_FIFTY = "^([1-9]\\d*)?((5|0)0)$";      //50倍数的正整数

    private static final String URL = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$";

    private static final String ALPHANUMERIC = "^[0-9A-Za-z]+$";    //只包含有数字或字母的字符串



    public static boolean isPhoneNumber(String phoneNumber) {
        return matchEntireRegion(PHONE_NUMBER_REGULAR, phoneNumber);
    }

    public static boolean isPhoneNumberNarrow(String phoneNumber) {
        return matchEntireRegion(PHONE_NUMBER_NARROW_REGULAR, phoneNumber);
    }

    public static boolean isMobileNumberNarrow(String phoneNumber) {
        return matchEntireRegion(MOBILE_NUMBER_NARROW_REGULAR, phoneNumber);
    }

    public static boolean isUploadURL(String urls) {
        return matchEntireRegion(UPLOAD_URL, urls);
    }

    public static boolean isZipCode(String zipCode) {
        return matchEntireRegion(ZIP_CODE_REGULAR, zipCode);
    }

    public static boolean isEmail(String email) {
        return matchEntireRegion(E_MAIL_REGULAR, email);
    }

    public static boolean isNumberPlates (String numberPlates) {
        return matchEntireRegion(NUMBER_PLATES_REGULAR, numberPlates);
    }

    public static boolean isAlphanumericChar (String alphanumeric) {
        return matchEntireRegion(ALPHANUMERIC_CHARACTER, alphanumeric);
    }

    public static boolean isBankNo (String bankNo) {
        return matchEntireRegion(BANK_NO, bankNo);
    }

    public static boolean isIdCardNumber (String idCardNumber) {
        return matchEntireRegion(ID_CARD_NUMBER, idCardNumber);
    }

    public static boolean isUrl (String url) {
        return matchEntireRegion(URL, url);
    }

    public static boolean isMultiplesOfFifty(String number) {
        return matchEntireRegion(MULTIPLES_OF_FIFTY, number);
    }

    public static boolean isAlphaNumeric(String chars) {
        return matchEntireRegion(ALPHANUMERIC, chars);
    }

    /**
     * 整个区域与模式匹配(匹配整个字符串)
     * @param regulation
     * @param matchingString
     * @return
     */
    public static boolean matchEntireRegion(String regulation, String matchingString) {
        Pattern pattern = Pattern.compile(regulation);
        Matcher m = pattern.matcher(matchingString);
        return m.matches();
    }

    public static void main(String[] args) {

//        String url = "https://www.haosou.com/asdfasdflwefosladf/%%%###";
//
//        System.out.println(RegularMatchUtils.isUrl(url));

    }
}
